import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class EditContents extends JPanel
{
	GraphView graphPanel;
	JPanel bottom;
	JFrame parent;
	
	public EditContents(GraphView graph, JFrame myParent)
	{
		super();
		//setPreferredSize(new Dimension(500,500));
		graphPanel=graph;
		//JPanel spacer=new JPanel();
		//spacer.setPreferredSize(new Dimension(300,300));
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		JLabel text=new JLabel("Click on a color to change it; Click on a label to remove it");
		parent=myParent;
		bottom=new JPanel();
		bottom.setAlignmentX(CENTER_ALIGNMENT);
		parent.validate();
		//add(spacer);
		//spacer.add(text);
		add(bottom);
		repaint();
		repaint();
		validate();
		parent.validate();
		parent.pack();
		
		
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		
		JPanel key=graphPanel.getKeyPanel();
		Component[] comp=key.getComponents();
		//LayoutManager gLayout = new GridLayout(comp.length, 2);
		LayoutManager gLayout = new BoxLayout(bottom,BoxLayout.Y_AXIS);
		bottom.setLayout(gLayout);
		Component[] remove=bottom.getComponents();
		for (int i=0;i<remove.length;i++)
		{
			bottom.remove(remove[i]);
		}
		for (int i=1;i<comp.length-1;i+=2)
		{
			JPanel back=new JPanel();
			comp[i]=new JLabel(((JLabel)comp[i]).getText());
			//((JLabel)comp[i]).addMouseListener(graphPanel.getGraphRemove((i%2), this));
			JButton removeButton=new JButton("Remove");
			removeButton.addActionListener(graphPanel.getGraphRemove((i%2)-1, this, parent));
			back.add(comp[i]);
			back.add(removeButton);
			bottom.add(back);
			
		}
		remove=bottom.getComponents();
		if (remove.length==0)
		{
			bottom.add(new JLabel("    Your graph is empty!!    "));
		}
		/*for (int i=0;i<(comp.length-1);i++)
		{
			System.out.println(comp[i]);
			bottom.add(comp[i]);
		}*/
		revalidate();
		parent.pack();
		revalidate();
	}
	
	
}